﻿/****************************** Module Header ******************************\
 * Module Name:  CatalogComboBox.cs
 * Module Version:  1.0.0 (2012-1-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for adding textbox controls.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UCF.Core.Web.CustomControls
{
    /// <summary>
    /// 文本框组控件
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TextBoxGroup runat=server></{0}:TextBoxGroup>")]
    public class TextBoxGroup : CompositeControl
    {
        private Panel panel;
        /// <summary>
        /// 获取或设置文本框组的值列表
        /// </summary>
        public IList<String> Values { get; set; }
        /// <summary>
        /// 获取或设置最多控件数量,默认是5个
        /// </summary>
        public Int32 MaxNumber { get; set; }
        /// <summary>
        /// 获取或设置按钮控件的样式类
        /// </summary>
        public String ButtonCssClass
        {
            get
            {
                String s = (String)ViewState["ButtonCssClass"];
                return (s == null) ? "" : s;
            }
            set
            {
                ViewState["ButtonCssClass"] = value;
            }
        }
        /// <summary>
        /// 获取或设置添加控件按钮的文本
        /// </summary>
        public string ButtonTextAdd
        {
            get
            {
                String s = (String)ViewState["ButtonTextAdd"];
                return (s == null) ? "" : s;
            }
            set
            {
                ViewState["ButtonTextAdd"] = value;
            }
        }
        /// <summary>
        /// 获取或设置移除控件按钮的文本
        /// </summary>
        public string ButtonTextRemove
        {
            get
            {
                String s = (String)ViewState["ButtonTextRemove"];
                return (s == null) ? "" : s;
            }
            set
            {
                ViewState["ButtonTextRemove"] = value;
            }
        }

        /// <summary>
        /// 创建子控件
        /// </summary>
        protected override void CreateChildControls()
        {
            if (this.MaxNumber == 0) this.MaxNumber = 5;
            if (this.Values == null) this.Values = new List<String>() { "" };
            if (this.FetchViewState("lstVal") == null) this.KeepViewState("lstVal", this.Values);
            this.Values = (IList<String>)this.FetchViewState("lstVal");

            TextBox tbInput;
            HtmlGenericControl oDiv;
            this.panel = new Panel();
            this.panel.ID = this.ID;

            foreach (String val in this.Values)
            {
                tbInput = new TextBox();
                tbInput.Text = val;
                if (this.CssClass.Length > 0) tbInput.CssClass = this.CssClass;
                oDiv = new HtmlGenericControl("div");
                oDiv.Controls.Add(tbInput);
                panel.Controls.Add(oDiv);
                oDiv.Dispose();
                tbInput.Dispose();
            }
            Button btnAdd = new Button();
            btnAdd.Text = (this.ButtonTextAdd.Length == 0) ? "添加" : this.ButtonTextAdd;
            btnAdd.Click += btnAdd_Click;
            Button btnRemove = new Button();
            btnRemove.Text = (this.ButtonTextRemove.Length == 0) ? "移除" : this.ButtonTextRemove;
            btnRemove.Click += btnRemove_Click;
            oDiv = new HtmlGenericControl("div");
            oDiv.Controls.Add(btnAdd);
            oDiv.Controls.Add(btnRemove);
            panel.Controls.Add(oDiv);
            this.Controls.Add(panel);
            btnAdd.Dispose();
            btnRemove.Dispose();
        }
        /// <summary>
        /// 添加控件操作事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            IList<String> lstValue = this.GetValues();
            if (lstValue.Count >= this.MaxNumber) return;
            lstValue.Add("");
            this.KeepViewState("lstVal", lstValue);
            this.RecreateChildControls();
        }
        /// <summary>
        /// 移除控件操作事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            IList<String> lstValue = this.GetValues();
            if (lstValue.Count < 2) return;
            lstValue.RemoveAt(lstValue.Count - 1);
            this.KeepViewState("lstVal", lstValue);
            this.RecreateChildControls();

        }

        /// <summary>
        /// 重新输出控件
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            foreach (Control oControl in this.Controls)
            {
                oControl.RenderControl(writer);
            }

        }
        /// <summary>
        /// 保存ViewState数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        private void KeepViewState(string name, object value)
        {
            ViewState[name] = value;
        }
        /// <summary>
        /// 获取ViewState数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private object FetchViewState(string name)
        {
            return ViewState[name];
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <returns></returns>
        public IList<String> GetValues()
        {
            IList<String> lstOut = new List<String>();
            TextBox tbInput;
            foreach (Control oDiv in this.panel.Controls)
            {
                if (oDiv.GetType() == typeof(HtmlGenericControl) && oDiv.Controls[0].GetType() == typeof(TextBox))
                {
                    tbInput = (TextBox)oDiv.Controls[0];
                    lstOut.Add(tbInput.Text);
                }
            }
            return lstOut;
        }
    }
}
